package com.avocado.base.validator.organization;

import com.avocado.base.repository.OrganizationRepository;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class OrganizationAddCodeValidator implements ConstraintValidator<OrganizationAddCodeValidation, String> {


    private final OrganizationRepository organizationRepository;

    public OrganizationAddCodeValidator(OrganizationRepository organizationRepository) {
        this.organizationRepository = organizationRepository;
    }

    @Override
    public void initialize(OrganizationAddCodeValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {


        try {



            long count=organizationRepository.getCountByCode(value);

            return count<1L;



        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage());
        }
        return false;
    }
}
